package ber.mcs;

import android.util.Log;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;

public class Server implements Runnable {
	public ArrayList<String> fila = new ArrayList<String>();
	DatagramSocket theSocket = null;
	public Client client = new Client();

	public Server() {

		try {
			theSocket = new DatagramSocket(1235);
			// System.out.println("Server created and waiting for incomming connections...");
			Log.d("UDP",
					"Server created and waiting for incomming connections...");
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			System.out.println("Error creating the socket: " + e.getMessage());
		}
	}

	public void run() {

		DatagramPacket theRecievedPacket;
		byte[] inBuffer;
		try {
			fila.add(null);
			Log.d("TESTE", "##");
			// create some space for the text to send and recieve data
			inBuffer = new byte[50];
			// create a place for the client to send data too
			theRecievedPacket = new DatagramPacket(inBuffer, inBuffer.length);
			// wait for a client to request a connection
			while (true) {
				theSocket.receive(theRecievedPacket);
				fila.add(new String(theRecievedPacket.getData(), 0,
						theRecievedPacket.getLength()));
				//Log.d("UDP", (String)fila.get(1));
			}
		} catch (IOException ExceIO) {
			System.out.println("Error with client request : "
					+ ExceIO.getMessage());
		}

		// close the server socket
	}
}
